﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Javavaj.iHunter.Filters
{
    public partial class ConfigDialog : Form
    {
        private XmlDocument newsettings = null;
        
        public ConfigDialog(XmlDocument settings)
        {
            InitializeComponent();
            SetDialogBySettings(settings);
        }

        public XmlDocument NewSettings
        {
            get
            {
                return newsettings;
            }
        }

        private void SetDialogBySettings(XmlDocument settings)
        {
            try
            {
                if (settings != null)
                {
                    XmlElement nodeWhite = settings.DocumentElement["WhiteList"];
                    XmlElement nodeBlack = settings.DocumentElement["BlackList"];
                    checkBoxWhite.Checked = bool.Parse(nodeWhite["Enable"].InnerText);
                    checkBoxBlack.Checked = bool.Parse(nodeBlack["Enable"].InnerText);
                    
                    foreach(XmlElement ele in nodeWhite.GetElementsByTagName("Word"))
                        listBoxWhite.Items.Add(ele.InnerText);
                    foreach(XmlElement ele in nodeBlack.GetElementsByTagName("Word"))
                        listBoxBlack.Items.Add(ele.InnerText);
                }
            }
            catch
            {
                checkBoxWhite.Checked = true;
                listBoxWhite.Items.Clear();
                checkBoxBlack.Checked = true;
                listBoxBlack.Items.Clear();
            }
        }

        private XmlDocument MakeSettingsByDialog()
        {
            XmlDocument settings = new XmlDocument();
            settings.LoadXml("<Settings><WhiteList><Enable>False</Enable></WhiteList><BlackList><Enable>False</Enable></BlackList></Settings>");

            XmlElement nodeWhite = settings.DocumentElement["WhiteList"];
            XmlElement nodeBlack = settings.DocumentElement["BlackList"];
            nodeWhite["Enable"].InnerText = checkBoxWhite.Checked.ToString();
            nodeBlack["Enable"].InnerText = checkBoxBlack.Checked.ToString();
            
            foreach ( string str in listBoxWhite.Items)
            {
                XmlElement node = settings.CreateElement("Word");
                node.InnerText = str;
                nodeWhite.AppendChild(node);
            }
            foreach (string str in listBoxBlack.Items)
            {
                XmlElement node = settings.CreateElement("Word");
                node.InnerText = str;
                nodeBlack.AppendChild(node);
            }

            return settings;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            newsettings = MakeSettingsByDialog();
        }

        private void buttonAddWhite_Click(object sender, EventArgs e)
        {
            string word = textBoxWhite.Text.Trim();

            if (word != "")
            {
                if (word.Length > 50)
                {
                    MessageBox.Show("The key word can't be so long.", "Keyword Filter Configuration");
                    return;
                }
                if (!listBoxWhite.Items.Contains(word))
                    listBoxWhite.Items.Add(word);
                if (listBoxBlack.Items.Contains(word))
                    listBoxBlack.Items.Remove(word);
                textBoxWhite.Text = "";
            }
        }

        private void buttonAddBlack_Click(object sender, EventArgs e)
        {
            string word = textBoxBlack.Text.Trim();

            if (word != "")
            {
                if (word.Length > 50)
                {
                    MessageBox.Show("The key word can't be so long.", "Keyword Filter Configuration");
                    return;
                }
                if (!listBoxBlack.Items.Contains(word))
                    listBoxBlack.Items.Add(word);
                if (listBoxWhite.Items.Contains(word))
                    listBoxWhite.Items.Remove(word);
                textBoxBlack.Text = "";
            }
        }

        private void buttonDeleteWhite_Click(object sender, EventArgs e)
        {
            if (listBoxWhite.SelectedIndex != -1)
                listBoxWhite.Items.RemoveAt(listBoxWhite.SelectedIndex);
        }

        private void buttonDeleteBlack_Click(object sender, EventArgs e)
        {
            if (listBoxBlack.SelectedIndex != -1)
                listBoxBlack.Items.RemoveAt(listBoxBlack.SelectedIndex);
        }

        private void buttonDeleteAllWhite_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you wanna clear all words?", "Keyword Filter Configuration", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                listBoxWhite.Items.Clear();
        }

        private void buttonDeleteAllBlack_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you wanna clear all words?", "Keyword Filter Configuration", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                listBoxBlack.Items.Clear();
        }

        private void textBoxWhite_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                buttonAddWhite_Click(null, null);
            if (e.KeyChar == (char)27)
                textBoxBlack.Text = "";
        }

        private void textBoxBlack_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                buttonAddBlack_Click(null, null);
            if (e.KeyChar == (char)27)
                textBoxBlack.Text = "";
        }
    }
}
